package com.lushu.chapitre1.core {

import com.sfdk.core.ConfigReader;
import com.sfdk.data.ServerKeyPair;
import com.sfdk.locale.DateFormatter;
import com.sfdk.locale.LocalisedMessageEngine;
import com.sfdk.locale.LocalisedToolTipEngine;
import com.sfdk.log.Logger;
import com.sfdk.structure.ArrayList;

import flash.events.EventDispatcher;

public final class Configuration extends EventDispatcher {
    public var list:ArrayList;
    public var version:String;
    public var locale:String;
    public var currentServer:ServerKeyPair;
    public var defaultServer:ServerKeyPair;

    private var _serverList:Vector.<ServerKeyPair>;
    private var _dateFormater:DateFormatter;
    private var _messageEngine:LocalisedMessageEngine;
    private var _tooltipEngine:LocalisedToolTipEngine;

    public function Configuration(configReader:ConfigReader):void {
        try {
            version = configReader.readConfig("version").toString();
            locale = configReader.readConfig("defaultLocale").toString();

            _serverList = new Vector.<ServerKeyPair>();
            var serverList:XML = configReader.readConfig("serverList");
            var tmpKeyPair:ServerKeyPair;

            for each(var server:XML in serverList.server) {
                tmpKeyPair = new ServerKeyPair();
                tmpKeyPair.serverName = server.serverName;
                tmpKeyPair.serverAddress = server.serverAddress;
                tmpKeyPair.serverPort = int(Number(server.serverPort));
                tmpKeyPair.gatewayDestination = server.gatewayDestination;
                _serverList.push(tmpKeyPair);
            }

            _dateFormater = new DateFormatter(locale);
            _messageEngine = new LocalisedMessageEngine(locale);
            _tooltipEngine = new LocalisedToolTipEngine(locale);

            currentServer = _serverList[0];
        }
        catch(e:Error) {
            Logger.log("Configuration :: There has been an error while reading the configuration " + e.message);
        }
    }
}
}